package com.xcity.game.adventure.model;

import com.xcity.db.entity.Player;
import com.xcity.game.adventure.AdventureUtils;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.player.robot.RobotUtils;
import com.xcity.util.IdUtils;

import naga.x.util.CommonUtils;

public enum ActionType {
	STEAL {
		@Override
		public ActionTarget find(ActionTargetQueue queue, Player source) {
			// 第一次/服务器总玩家数低于20时取机器人
			if (source.getPool().getIntValue(PlayerPool.PROPERTY_ADVENTURE_STEPS) != 0
					&& IdUtils.sizeOfPlayer() > 20) {
				int retrys = 0;
				while (retrys++ < 10) {
					ActionTarget target = find0(queue, source);
					if (target != null) {
						return target;
					}
				}
			}
			// 没找到则取机器人
			Player robot = RobotUtils.getRandomly();
			int[] moneyRegion = AdventureUtils.getStealGoldRegion(source);
			int money = CommonUtils.randomInt(moneyRegion[0], moneyRegion[1]);
			return new ActionTarget(robot.getId(), money);
		}
		
		private ActionTarget find0(ActionTargetQueue queue, Player source) {
			Player target = queue.poll();
			if (target != null) {
				if (!AdventureUtils.canBeSteal(source, target, queue)) {
					queue.offer(target, true);
					return null;
				}
				byte c = queue.countAdd(target);
				if (c < AdventureUtils.maxCountBeTarget) {
					queue.offer(target, true);
				}
				int[] moneyRegion = AdventureUtils.getStealGoldRegion(source);
				// 随机可偷取的金额
				int money = CommonUtils.randomInt(moneyRegion[0], moneyRegion[1]);
				return new ActionTarget(target.getId(), money);
			}
			return null;
		}
	},
	ATTACK {
		@Override
		public ActionTarget find(ActionTargetQueue queue, Player source) {
			// 新手引导/服务器总玩家数低于20时取机器人
			if (source.getPool().getIntValue(PlayerPool.PROPERTY_ADVENTURE_STEPS) != 1
					&& IdUtils.sizeOfPlayer() > 20) {
				int retrys = 0;
				while (retrys++ < 10) {
					ActionTarget target = find0(queue, source);
					if (target != null) {
						return target;
					}
				}
			}
			// 没找到则取机器人
			Player robot = RobotUtils.getRandomly();
			return new ActionTarget(robot.getId());
		}
		private ActionTarget find0(ActionTargetQueue queue, Player source) {
			Player target = queue.poll();
			if (target != null) {
				if (!AdventureUtils.canBeAttack(source, target, queue)) {
					queue.offer(target, true);
					return null;
				}
				byte c = queue.countAdd(target);
				if (c < AdventureUtils.maxCountBeTarget) {
					queue.offer(target, true);
				}
				return new ActionTarget(target.getId());
			}
			return null;
		}
	},
	CAPTURE {
		@Override
		public ActionTarget find(ActionTargetQueue queue, Player source) {
			// 新手引导为机器人
			if (AdventureUtils.getAdventureCaptureFuncCount(source) == 0) {
				return new ActionTarget(RobotUtils.getRandomly().getId());
			}
			// 总玩家数低于20时屏蔽此功能
			if (IdUtils.sizeOfPlayer() > 20) {
				int retrys = 0;
				while (retrys++ < 5) {
					ActionTarget target = find0(queue, source);
					if (target != null) {
						return target;
					}
				}
			}
			return null;
		}
		private ActionTarget find0(ActionTargetQueue queue, Player source) {
			Player target = queue.poll();
			if (target != null) {
				if (!AdventureUtils.canBeCapture(source, target, queue)) {
					queue.offer(target, true);
					return null;
				}
				byte c = queue.countAdd(target);
				if (c < AdventureUtils.maxCountBeTarget) {
					queue.offer(target, true);
				}
				return new ActionTarget(target.getId());
			}
			return null;
		}
	};

	public abstract ActionTarget find(ActionTargetQueue queue, Player source);

}
